package com.xiaohu.myspace.buiness;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.xiaohu.myspace.utils.DBUtils;

public class BaseBuiness {

	protected static final int pageSize = 30;

	public interface DoInBuiness<T> {
		public T doSomeThingInBuiness(SessionFactory sessionFactory,
				Session session);
	}

	public synchronized <T extends Object> T doSomeThingInDB(
			DoInBuiness<T> doInBuiness) {

		SessionFactory sessionFactory = DBUtils.getSessionFactoryFromSpring();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
        T returnData=null;
		try {
			returnData=doInBuiness.doSomeThingInBuiness(sessionFactory, session);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		}finally{
			session.close();
		}
		return returnData;
	}

}
